package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.Medida;


public class DAOMedida {

        public Set<Medida> select() throws Exception {
                Set<Medida> objs = new HashSet<Medida>();
                try {

                        String query = "select idMedida, nome from Medida";

                        Statement statement = DAOConnection.getConnection().createStatement();

                        ResultSet resultSet = statement.executeQuery(query);

                        while(resultSet.next()) {
                                Medida obj = new Medida();
                                obj.idMedida = resultSet.getInt(1);
                                obj.nome = resultSet.getString(2);
                                objs.add(obj);
                        }
                } catch(SQLException e) {
                        e.printStackTrace();
                        throw new Exception();
                } finally {
                        DAOConnection.closeConnection();
                }
                return objs;
        }

        public Medida select(int id) throws Exception {
                Medida obj = null;
                try     {

                        String query = "select idMedida, nome from Medida where idMedida = ?";

                        PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
                        statement.setInt(1, id);
                        ResultSet resultSet = statement.executeQuery();

                        while(resultSet.next()) {
                                obj = new Medida();
                                obj.idMedida = resultSet.getInt(1);
                                obj.nome = resultSet.getString(2);
                          
                        }
                } catch(SQLException e) {
                        e.printStackTrace();
                        throw new Exception();
                } finally {
                        DAOConnection.closeConnection();
                }
                return obj;
        }

        public void insert(Medida f) throws Exception {
                try {
                        String insert = "insert into Medida(nome) values(?)";

                        PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

                        statement.setString(1, f.nome);
                 

                        statement.executeUpdate();
                } catch(SQLException e) {      
                        throw new Exception();
                } finally {
                        DAOConnection.closeConnection();
                }

        }

        public void update(Medida f) throws Exception {
                try {

                        String update = "update Medida set nome = ?  where idMedida = ?";

                        PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

                        statement.setString(1, f.nome);
                        statement.setInt(2, f.idMedida);
                        


                        statement.executeUpdate();
                } catch(SQLException e) {
                        throw new Exception();
                } finally {
                        DAOConnection.closeConnection();
                }

        }

        public void delete(Medida f) throws Exception {
                try {

                        String delete = "delete from Medida where idMedida = ?";

                        PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

                        statement.setInt(1, f.idMedida);

                        statement.executeUpdate();
                } catch(SQLException e) {
                        throw new Exception();
                } finally {
                        DAOConnection.closeConnection();
                }
        }

}
